package com.houdoudou.customer.buy;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.houdoudou.bean.Order;
import com.houdoudou.bean.OrderProduct;
import com.houdoudou.bean.Product;
import com.houdoudou.customer.R;

import com.houdoudou.customer.common.AsynImageLoader;
import com.houdoudou.customer.common.CommonFunction;
import com.houdoudou.customer.common.Constant;

import com.houdoudou.customer.common.ListViewUtil;
import com.houdoudou.customer.common.UniversalHttpPost;


public class OrderDetailAct  extends FragmentActivity{


	public static JSONObject orderJO;
	public static JSONArray  productsJA; 
	View.OnClickListener myClickListener;
	ImageButton back;
	FrameLayout container;
    FragmentManager myFragmentMan ;
    Fragment current_frg,frg_detail,frg_state,frg_refund,frg_comment;
    TextView detail,state,refund;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.act_orderdetail);
		orderJO= JSONObject.fromObject(getIntent().getStringExtra("orderJO"));
		System.out.println("act playce order oncreate  orderJO="+orderJO);
		productsJA= orderJO.getJSONArray("products");
		initListenr();	
		back= (ImageButton) findViewById(R.id.back_act_orderdetail);
	    back.setOnClickListener(myClickListener);
		container= (FrameLayout) findViewById(R.id.fl_container_orderdetailact);
		myFragmentMan = getSupportFragmentManager();
		detail= (TextView) findViewById(R.id.tv_detail_actorderdetail);
		refund=(TextView) findViewById(R.id.tv_refund_actorderdetail);
		state=(TextView) findViewById(R.id.tv_state_actorderdetail);
		refund.setOnClickListener(myClickListener);
		state.setOnClickListener(myClickListener);
		detail.setOnClickListener(myClickListener);
		clearFrgButton();
		  IntentFilter filter = new IntentFilter(ChoosePayAct.ACTION_ORDER_CHANGED);  
	      registerReceiver(broadcastReceiver, filter);  

		  
		state.performClick();
		}
	
	 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  
		
		       @Override  
		       public void onReceive(Context context, Intent intent) {  
	            // TODO Auto-generated method stub  
		    	   String orderJOStr= intent.getExtras().getString("order");
		    	   orderJO= JSONObject.fromObject(orderJOStr);	
		    	   System.out.println("OrderDetailAct broadcastReceiver order id="+orderJO.getLong(Order.ID));
	         
		       }  
		   };  


	 private void initListenr(){
		 myClickListener= new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
		      if(v==back){
		    	  new Thread () { 
						public void run () { 
						try { 									
						Instrumentation inst= new Instrumentation(); 
						inst.sendKeyDownUpSync(KeyEvent. KEYCODE_BACK); 
						} catch(Exception e) { 
						e.printStackTrace(); 
						         } 
						  } 
						}.start(); }	
		      if(v==detail){
		    	  clearFrgButton();
		    	  detail.setBackgroundColor(Constant.WhileBlueColor);
		    	  detail.setTextColor(Constant.MaintThemeColor);
		    	  if(frg_detail==null){ frg_detail= new OrderDetailFrg();}
		    	  switchContent(frg_detail);
		         }
		      if(v==state){
		    	  clearFrgButton();
		    	  state.setBackgroundColor(Constant.WhileBlueColor);
		    	  state.setTextColor(Constant.MaintThemeColor);
		    	  if(frg_state==null){ frg_state= new OrderStateFrg();}
		    	  switchContent(frg_state);
		         }
		      if(v==refund){
		    	  clearFrgButton();
		    	  refund.setBackgroundColor(Constant.WhileBlueColor);
		    	  refund.setTextColor(Constant.MaintThemeColor);
		    	  if(frg_refund==null){ frg_refund= new OrderRefundFrg();}
		    	  switchContent(frg_refund);
		         }
			}
			
			
		};
	 }	

	 private void clearFrgButton(){
		 detail.setTextColor(Constant.WhileColor);
		 detail.setBackgroundColor(Constant.MaintThemeColor);
		 state.setTextColor(Constant.WhileColor);
		 state.setBackgroundColor(Constant.MaintThemeColor);
		 refund.setTextColor(Constant.WhileColor);
		 refund.setBackgroundColor(Constant.MaintThemeColor);
	 }
	 public void switchContent(Fragment to) {
		   FragmentTransaction transaction = myFragmentMan.beginTransaction();
		   if(current_frg==null){
			   current_frg = to;
			   transaction.replace(R.id.fl_container_orderdetailact, current_frg).commit();
			 
		   }else if (current_frg!= to){
	         
	          //  FragmentTransaction transaction = myFragmentMan.beginTransaction().setCustomAnimations(
	            //        android.R.anim.fade_in, android.R.anim.fade_out);
	         
	            if (!to.isAdded()) {    // 先判断是否被add过
	                transaction.hide(current_frg).add(R.id.fl_container_orderdetailact, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
	            } else {
	                transaction.hide(current_frg).show(to).commit(); // 隐藏当前的fragment，显示下一个
	            }
	        current_frg = to;  
		   }
		   
	    }

		@Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			 unregisterReceiver(broadcastReceiver);  
			super.onDestroy();
		}
}
